<!--运营单位选择通用组件  - -->
<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="destoryClose"
    @cancel="handleCancel"
    @ok="handleOk"
    cancelText="关闭">
        <a-card :bordered="false">
          <!-- 树控件区域-->
          <template>
            <a-dropdown :trigger="[dropTrigger]" @visibleChange="dropStatus">
            <span style="user-select: none">
              <lone-asnc-tree ref="loneAsncTree"
                              :url="url"
                              :initDataFatherFunction="initDataFatherFunction"
                              :searchKeyWordFunction="searchKeyWordFunction"
                              :dropTrigger="dropTrigger"
                              :buttonTitle="buttonTitle"
                              :permissionData="permissionData"
                              :permissionKeys="permissionKeys"
                              :placeholder="placeholder"
                              :icon="icon"
                              :selectedKeyData="selectedKeyData"
                              :checkable="checkable"
                              :multiple="multiple" :db-click="`Y`"
                              @rightHandle="rightHandle"
                              @dropStatus="dropStatus"
                              @handleTreeAdd="handleTreeAdd"
                              @handleTreeEdit = "handleTreeEdit"
                              @handleTreeDelete ="handleTreeDelete"
                              @closeDrop="closeDrop"
                              @onSelect="onSelect"
                              @onDbSelect="onDbSelect"
              />
            </span>
              <!--新增右键点击事件,增加添加编辑删除功能-->
            </a-dropdown>
          </template>
        </a-card>
      <!--表单区域-->
  </a-modal>
</template>

<script>
  import LoneAsncTree from './LoneAsncTree'
  import ACol from 'ant-design-vue/es/grid/Col'
  import { getAction} from '@lone/lone-vue/src/api/manage'
  import ARow from 'ant-design-vue/es/grid/Row'
  import { LoneAsncTreeMixin } from '@/mixins/LoneAsncTreeMixin'
  export default {
    mixins: [LoneAsncTreeMixin],
    components: {
      ARow,
      ACol,
      LoneAsncTree,
    },
    data () {
      return {
        buttonName:'物资',
        loading:false,
        title:'机构选择',
        visible:false,
        code:'',
        confirmLoading: false,
        destoryClose:true,
        autoLoad:false,
        modalWidth:800,
        dropTrigger: 'contextmenu',
        rightClickSelectedKey:'',
        url: {
          list: '/sysdepart/sysDepart/queryTreeList',
          search: '/sysdepart/sysDepart/searchBy'
        },
        //控制按钮是否显示  增删改，0 1 2 默认开启，
        permissionKeys: [
          {
            addKey: '4',
            editKey: '4',
            deleteKey: '4',
            refreshKey: '4',
          }
        ],
        //按钮权限控制
        permissionData: [
          {
            dataPermissionAdd: '',
            dataPermissionEdit: '',
            dataPermissionDelete: '',
            dataPermissionRefresh: '',
          }
        ],
        //按钮内容
        buttonTitle:'单位分类',
        placeholder:'请输入查找的单位名称',
        onSelectKey:'no',
        //树形控件定制图标
        icon: [
          {
            //叶子节点
            slot :'smile',//图标分模块化
            linkType : 'pic-right',//图标类型
            style :'color:green' //图标显示颜色
          },{
            //非叶子结点
            slot :'meh',//图标分模块化
            linkType : 'pic-left',//图标类型
            style :'color:red' //图标显示颜色
          }
        ],
        multiple:false,
        checkable:false,
        selectedKeyData:'yes',
        //保存选中的Key
        treeData:'',
      }
    },
    created() {
    },
    props:{
      disabledData:{
        type:Boolean,
        required:false,
        default:true
      },
      orgType:{
        type:String,
        required:false,
        default:''
      }
    },
    watch:{
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false
      },
    },

    methods: {
      showSelectDepartTreeModal(code) {
        this.visible = true
        this.code = code
      },
        initDataFatherFunction (key = null, first = true, url = this.url.list) {
        let that = this
          getAction(url,{ key,orgType:that.orgType }).then((res) => {
            if (res.success) {
              // 禁用父节点
              that.$refs.loneAsncTree.dataSource = res.result
              that.$refs.loneAsncTree.loading = false
            }
          })
      },
      searchKeyWordFunction(value){
        if (value) {
          getAction(this.url.search,{ keyWord:value,}).then((res) => {
            if (res.success) {
              this.$refs.loneAsncTree.dataSource = res.result
              this.$refs.loneAsncTree.loading = false
            }
          })
        }else{
          this.initData()
        }
      },
      modalFormOk(){
        this.initData();
      },
      onClose(){
        this.selectKeys = ''
      },
      onSelect(treeData){
        if(treeData != undefined){
          this.treeData = treeData;
        }
      },
      handleOk() {
        this.$emit(this.code, this.treeData)
        this.visible = false
        this.code = ''
      },
      onDbSelect(selectKey,record){
        this.$emit(this.code,record)
        this.visible = false
      },
      handleCancel(){
        this.visible = false
      }
    },
  }
</script>
